﻿using System;
using System.Web;
using System.Web.UI;
using StampController.BL;
using StampController.BL.Models;

namespace StampController.Pages
{
    public class BasePage : Page
    {
        protected string UserName
        {
            get
            {
                if (HttpContext.Current.User.Identity.Name.Contains("\\"))
                {
                    return HttpContext.Current.User.Identity.Name.Split('\\')[1];
                }
                return HttpContext.Current.User.Identity.Name;
            }
        }

        protected override void OnLoad(EventArgs e)
        {

            if (!IsPostBack)
            {
                var user = SessionUtils.Get<UsersModel>("USER");
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    if (user == null)
                    {
                        var userImpl = new UsersImpl();
                        user = userImpl.GetUser(UserName);
                        SessionUtils.Set("USER", user);
                    }
                }
                else
                {
                    Session.Abandon();
                }
            }

            base.OnLoad(e);
        }
    }
}